<template>
  <div class="addfun wh100 common-content p15">
    <div class="wh100">
      <el-scrollbar style="height: 100%">
        <el-form
            :model="form"
            :rules="basicRules"
            ref="basicForm"
            label-width="150px"
            class="demo-ruleForm">
          <el-row>
            <el-col>
              <el-form-item label="函数名称：" prop="pluginsName">
                <el-input v-model="form.funName" placeholder="请填写函数名称" style="width: 300px;"></el-input>
              </el-form-item>
            </el-col>

            <el-col>
              <el-form-item label="函数入参：">
                <div class="param-box-group">
                  <el-table
                      class="yt-table"
                      ref="putTable"
                      :data="putDatas"
                      border
                      size="mini"
                      style="width: 100%"
                      row-key="id"
                      max-height="300px"
                      default-expand-all
                      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                    <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
                    <el-table-column prop="name" label="参数类型" width="300px" header-align="center">
                      <template slot-scope="scope">
                        <div class="table-select-box">
                          <el-select
                              class="w100"
                              size="mini"
                              v-model="scope.row.name"
                              placeholder="请选择参数类型"
                              @change="putTypeChange($event,scope.row,scope.$index,scope)">
                            <el-option :label="item.className"
                                       :value="item.id"
                                       v-for="(item, index) in paramTypeLists"
                                       :key="index"></el-option>
                          </el-select>
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column prop="value" label="参数名称" width="300px" align="center">
                      <template slot-scope="{row,$index}">
                        <el-input v-model="row.value" size="mini" placeholder="请填写参数名称"></el-input>
                      </template>
                    </el-table-column>
                    <el-table-column prop="" label="操作" min-width="300px" header-align="center" align="left">
                      <template slot-scope="scope">
                        <el-button
                            type="success"
                            size="mini"
                            icon="el-icon-circle-plus-outline"
                            v-if="scope.row.showAdd"
                            @click="addPut(scope.row,scope.$index)">参数
                        </el-button>
                        <el-button
                            type="success"
                            size="mini"
                            v-if="scope.row.type==1"
                            @click="addStructuralPut(scope.row,scope.$index)"
                            icon="el-icon-circle-plus-outline">子参数
                        </el-button>
                        <el-button
                            type="danger"
                            size="mini"
                            v-if="scope.row.showDel"
                            icon="el-icon-remove-outline"
                            @click="delPut(scope.row,scope.$index)">删除
                        </el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </el-form-item>
            </el-col>

            <el-col>
              <el-form-item label="函数出参：">
                <div class="param-box-group">
                  <el-table
                      class="yt-table"
                      ref="outTable"
                      :data="outDatas"
                      border
                      size="mini"
                      style="width: 100%"
                      max-height="300px"
                      row-key="id"
                      default-expand-all
                      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                    <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
                    <el-table-column prop="" label="参数类型" width="300px" header-align="center">
                      <template slot-scope="scope">
                        <div class="table-select-box">
                          <el-select
                              class="w100"
                              size="mini"
                              v-model="scope.row.name"
                              placeholder="请选择参数类型"
                              @change="putTypeChange($event,scope.row,scope.$index,scope)">
                            <el-option :label="item.className"
                                       :value="item.id"
                                       v-for="(item, index) in paramTypeLists"
                                       :key="index"></el-option>
                          </el-select>
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column prop="value" label="参数名称" width="300px" align="center">
                      <template slot-scope="{row,$index}">
                        <el-input v-model="row.value" size="mini" placeholder="请填写参数名称"></el-input>
                      </template>
                    </el-table-column>
                    <el-table-column prop="" label="操作" min-width="300px" header-align="center" align="left">
                      <template slot-scope="{row,$index}">
                        <el-button
                            type="success"
                            size="mini"
                            @click="addOut(row,$index)"
                            icon="el-icon-circle-plus-outline"
                            v-if="row.type==1">子参数
                        </el-button>
                        <el-button
                            type="danger"
                            size="mini"
                            v-if="row.showDel"
                            icon="el-icon-remove-outline"
                            @click="delOut(row,$index)">删除
                        </el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </el-form-item>
            </el-col>

            <el-col>
              <el-form-item label="备注">
                <el-input type="textarea" v-model="form.remarks"></el-input>
              </el-form-item>
            </el-col>

            <el-col>
              <el-form-item label="">
                <div class="text-center">
                  <el-button size="small" type="primary" @click="addFun">确 定</el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-scrollbar>
    </div>
  </div>
</template>

<script>
import {
  get_getPClass,
  post_updateFunction,
  put_funactonAdd,
  del_deletePFunction,
  get_getPLibrarySelect
} from "@/apis/assembly/assembly";

let id = 1000
export default {
  name: "addfun",
  data() {
    return {
      paramTypeLists: [],
      form: {
        funName: '',
        remarks: '',
      },
      basicRules: {},
      putDatas: [{
        id: id,
        type: '',
        name: '',
        value: '',
        level: 1,
        showAdd: true,
        showDel: false,
        children: []
      }],
      outDatas: [{
        id: id,
        type: '',
        name: '',
        value: '',
        children: []
      }]
    }
  },
  mounted() {
    this.getType()
  },
  methods: {
    // 获取组件函数参数类型
    getType() {
      get_getPClass().then((res) => {
        this.paramTypeLists = res.data.data;
      });
      get_getPLibrarySelect().then((res) => {
        this.Plx = res.data.data;
      });
    },
    // ------ 入参 -----
    putTypeChange(val, row, index, scope) {
      let tpyeItem = this.paramTypeLists.find(item => item.id == val)
      if (tpyeItem) {
        row.type = tpyeItem.classType
        row.typeName = tpyeItem.className
      }
    },
    addPut(row, index) {
      id += 1
      let newChild = {
        id: id,
        type: '',
        name: '',
        value: '',
        level: row.level,
        showAdd: true,
        showDel: false,
        children: []
      }
      this.putDatas = this.insertChild(this.putDatas, row.id, newChild)

      this.putDatas.forEach((item, index) => {
        if (index === this.putDatas.length - 1) {
          item.showAdd = true
        } else {
          item.showAdd = false
        }
        if (this.putDatas.length > 1) {
          item.showDel = true
        } else {
          item.showDel = false
        }
      })

      this.$nextTick(() => {
        this.tobottom()
      })
    },
    addStructuralPut(row, index) {
      id += 1
      let newChild = {
        id: id,
        type: '',
        name: '',
        value: '',
        level: row.level + 1,
        showDel: true,
        children: []
      }
      row.children.push(newChild);
    },
    delPut(row, index) {
      this.putDatas = this.remoteChild(this.putDatas, row.id)

      this.putDatas.forEach((item, index) => {
        if (index === this.putDatas.length - 1) {
          item.showAdd = true
        } else {
          item.showAdd = false
        }
        if (this.putDatas.length > 1) {
          item.showDel = true
        } else {
          item.showDel = false
        }
      })
    },
    // ----------------------
    insertChild(list, id, newChild) {
      list.forEach(item => {
        let flag = item.id === id // 过滤非匹配id
        if (flag) {
          list.push(newChild)
        } else {
          if (item.children.length > 0) {
            item.children = this.insertChild(item.children, id, newChild)
          }
        }
      })
      return list
    },
    remoteChild(list, id) {
      list = list.filter(item => {
        let flag = item.id != id //过滤非匹配id
        if (flag) {
          if (item.children.length > 0) {
            item.children = this.remoteChild(item.children, id)
          }
          return true
        } else {
          return flag
        }
      })
      return list
    },
    // ------ 出参 -----
    // 添加出参
    addOut(row, index) {
      id += 1
      let newChild = {
        id: id,
        type: '',
        name: '',
        value: '',
        showDel: true,
        children: []
      }
      row.children.push(newChild);
    },
    delOut(row, index) {
      this.outDatas = this.remoteChild(this.outDatas, row.id)
    },

    tobottom() {
      this.timer = setInterval(() => {
        let height = this.$refs.putTable.bodyWrapper.offsetHeight
        let osHeight = this.$refs.putTable.bodyWrapper.scrollHeight
        let osTop = this.$refs.putTable.bodyWrapper.scrollTop
        let ispeed = Math.floor((osHeight - height - osTop + 5) / 5)
        this.$refs.putTable.bodyWrapper.scrollTop = osTop + ispeed
        if (osTop === (osHeight - height)) {
          clearInterval(this.timer)
        }
      }, 30)
    },

    // 确定新增
    addFun() {
      let opt = {
        PId: this.$route.query.id,
        functionName: this.form.funName,
        remarks: this.form.remarks,
        paramInputClass: this.$refs.putTable.data.map(item => {
          return item.typeName + ' ' + item.value
        }).join(","),
        paramOutputClass: this.$refs.outTable.data.map(item => {
          return item.typeName + ' ' + item.value
        }).join(","),
      };

      console.log(this.$refs.putTable.data)

      console.log(opt)
      // put_funactonAdd(opt).then((res) => {
      //   let {code, msg} = res.data;
      //   if (code == 0) {
      //     this.$message.success(msg);
      //     this.getInfo();
      //     this.closeDia();
      //   }
      // });
    }
  }
}
</script>

<style scoped lang="scss">
  .addfun {
    overflow: hidden;
  }

  .param-box-group {
    display: flex;
    flex-wrap: wrap;

    .param-item {
      display: block;
      min-width: 100px;
      padding: 0 10px;
      text-align: center;
      border: 1px solid #216892;
      line-height: 30px;
      cursor: pointer;
      position: relative;
      border-radius: 4px;
      color: #ffffff;
      margin-bottom: 5px;
      margin-right: 5px;
      transition: all 0.3s;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;


      &:hover {
        background-color: #216892;
      }

      .el-icon-circle-close {
        position: absolute;
        right: 5px;
        top: 5px;
        cursor: pointer;
      }
    }
  }

  .table-select-box {
    display: inline-block;
  }
</style>